package com.v2.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.jakewharton.rxbinding3.view.RxView;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.v2.request.RequestCenter;
import com.v2.utils.TToast;
import com.v2.utils.okhttp.OkHttpRequestCallback;
import com.zou.lotterylucky.R;
import com.zou.lotterylucky.activity.MainActivity;
import com.zou.lotterylucky.adapters.BetsListAdapter;
import com.zou.lotterylucky.data.Order;
import com.zou.lotterylucky.databinding.ActivityDealDetailBinding;

import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import kotlin.Unit;

/*v1.0 done*/
public class OrderDetailActivity extends BaseActivity<ActivityDealDetailBinding> {
    @Override
    protected int layoutId() {
        return R.layout.activity_deal_detail;
    }

    private Order order;

    @SuppressLint({"SetTextI18n", "CheckResult"})
    @Override
    protected void onCreate() {
        mBinding.topbar.setTitle(getString(R.string.dingdanxiangqing));
        mBinding.topbar.addLeftBackImageButton().setOnClickListener(v -> onBackPressed());
        order = selectedOrder;
        mBinding.textOrder.setText(order.getSerialNumber());
        mBinding.textTime.setText(getString(R.string.xiadanshijian_dot) + order.getTimeStrAuto() + " Wifi");
        mBinding.textBelong.setText(getString(R.string.diqudot) + order.getBelongTo());

        if (order.isWinner() == 1) {
            mBinding.textPrize.setText(getString(R.string.prize) + new DecimalFormat("0.00").format(order.getWinningAmount()));

            if (getIntent().getIntExtra("from", -1) == 0) {
                mBinding.btnDelete.setVisibility(View.GONE);
            } else {
                mBinding.btnDelete.setVisibility(View.VISIBLE);
            }

            if (order.isPaid() == 1) {
                mBinding.btnDelete.setText(getString(R.string.paid));
                mBinding.btnDelete.setEnabled(false);
                mBinding.btnDelete.setBackgroundColor(getResources().getColor(R.color.kprogresshud_grey_color));
            } else {
                mBinding.btnDelete.setText(getString(R.string.redeem));
            }
        } else {
            if (getIntent().getIntExtra("from", -1) == 1) {
                mBinding.btnDelete.setText(getString(R.string.no_prize_number));
                mBinding.btnDelete.setEnabled(false);
                mBinding.btnDelete.setBackgroundColor(getResources().getColor(R.color.kprogresshud_grey_color));
            } else {
                mBinding.textPrize.setVisibility(View.GONE);
                mBinding.text2.setVisibility(View.GONE);
                mBinding.btnDelete.setVisibility(View.VISIBLE);
                mBinding.btnXiaDan.setText(getString(R.string.xiugai));
            }
        }

        mBinding.rcv.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rcv.setAdapter(new BetsListAdapter(order.getBets()));

        RxView.clicks(mBinding.btnXiaDan).throttleFirst(1000L, TimeUnit.MILLISECONDS).subscribe(new Consumer<Unit>() {
            @Override
            public void accept(Unit unit) throws Exception {
                String editBelongTo = editTimeLimit(order.getBelongTo());
                if (!TextUtils.isEmpty(editBelongTo)) {
                    selectedOrder.setBelongTo(editBelongTo);
                    Intent intent = new Intent(OrderDetailActivity.this, MainActivity.class);
                    intent.putExtra("Modify", true);
                    startActivityForResult(intent, 0);
                }
            }
        });

        mBinding.btnDelete.setOnClickListener(v -> {
            if (order.isWinner() == 1) {
                payOrder();
            } else {
                if (deleteTimeLimit(order)) {
                    QMUIDialog.MessageDialogBuilder dialogBuilder = new QMUIDialog.MessageDialogBuilder(this);
                    dialogBuilder.setTitle(getString(R.string.tishi));
                    dialogBuilder.setMessage(getString(R.string.confirm_delete));
                    dialogBuilder.addAction(getString(R.string.cancel), (dialog, index) -> dialog.dismiss());
                    dialogBuilder.addAction(0, getString(R.string.delete), QMUIDialogAction.ACTION_PROP_NEGATIVE, (dialog, index) -> {
                        RequestCenter.deleteOrderById("" + order.getId(), new OkHttpRequestCallback() {
                            @Override
                            public void onSuccess(String result) {
                                allOrders.remove(order);
                                TToast.show(getString(R.string.delete_success));
                            }

                            @Override
                            public void onFailure(String error) {
                                TToast.show(error);
                            }
                        });
                    });
                    dialogBuilder.create().show();
                }
            }
        });

        mBinding.textBetsCount.setText(getString(R.string.dingdanshu_dot) + (order.getBets().size() * order.getRegionCounts()));
        mBinding.textMoneyCount.setText(getString(R.string.jine_dot) + (new DecimalFormat("0.00").format(order.getTotal())));
    }

    private void payOrder() {
        RequestCenter.payOrder("" + order.getId(), new OkHttpRequestCallback() {
            @Override
            public void onSuccess(String result) {
                order.setPaid(1);
                mBinding.btnDelete.setEnabled(false);
                mBinding.btnDelete.setBackgroundColor(getResources().getColor(R.color.kprogresshud_grey_color));
            }

            @Override
            public void onFailure(String error) {
                TToast.show(error);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            this.setResult(RESULT_OK);
            this.finish();
        }
    }
}
